﻿Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.Collections.ObjectModel

Public Class TableStyle

#Region "Privates"
    Dim _name As String
    Dim _id As String
    Dim _content, _header, _contentbold, _contentspecial, _title As Font

    Dim _headerBackground As BaseColor
    Dim _headerBorderWidths As CellBorders(Of Single)
    Dim _headerBorderColors As CellBorders(Of BaseColor)

    Dim _cellbackground As Collection(Of BaseColor)
    Dim _contentBorderWidths As CellBorders(Of Single)
    Dim _contentBorderColors As CellBorders(Of BaseColor)

    Dim _lastRowBackground As BaseColor
    Dim _lastRowBorderWidths As CellBorders(Of Single)
    Dim _lastRowBorderColors As CellBorders(Of BaseColor)

    Dim _headerOnNewPage As Boolean
    Dim _takeLastRowBackgroundFromContentSeries As Boolean
#End Region

#Region "Constructor"
    Public Sub New(Name As String)
        _name = Name
        _id = Guid.NewGuid().ToString()
        _headerOnNewPage = False

        _cellbackground = New Collection(Of BaseColor)
        _headerBackground = BaseColor.WHITE

        _headerBorderWidths = New CellBorders(Of Single)(0.5)
        _contentBorderWidths = New CellBorders(Of Single)(0.5)

        _headerBorderColors = New CellBorders(Of BaseColor)(BaseColor.BLACK)
        _contentBorderColors = New CellBorders(Of BaseColor)(BaseColor.BLACK)

        _header = New Font(FontFactory.GetFont("Arial" + " bold", 11))
        _title = New Font(FontFactory.GetFont("Arial" + " bold", 15))
        _content = New Font(FontFactory.GetFont("Arial", 11))
        _contentbold = New Font(FontFactory.GetFont("Arial" + " bold", 11))
        _contentspecial = New Font(FontFactory.GetFont("Arial" + " bold", 11))
        _contentspecial.Color = BaseColor.RED

        _lastRowBackground = BaseColor.WHITE
        _lastRowBorderWidths = New CellBorders(Of Single)(0.5)
        _lastRowBorderColors = New CellBorders(Of BaseColor)(BaseColor.BLACK)
        _takeLastRowBackgroundFromContentSeries = True
    End Sub
#End Region

#Region "Properties"
    ''' <summary>
    ''' Represents a unique id of the object
    ''' </summary>
    ''' <value></value>
    ''' <returns>Returns a String containing the id</returns>
    ''' <remarks>The id is generated after initializing the object</remarks>
    Public ReadOnly Property Id As String
        Get
            Return _id
        End Get
    End Property

    ''' <summary>
    ''' Represents the name you specified during the initialization of this object
    ''' </summary>
    ''' <value></value>
    ''' <returns>Returns a String</returns>
    ''' <remarks></remarks>
    Public Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the font for the head line
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FontTitle As Font
        Get
            Return _title
        End Get
        Set(value As Font)
            _title = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the font for each cell in the table (except for headers)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default value is Arial, 11pt, normal, black.</remarks>
    Public Property FontContent As Font
        Get
            Return _content
        End Get
        Set(value As Font)
            _content = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the font for the horizontal (and if selected vertical) header cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default value is Arial, 13pt, bold, black.</remarks>
    Public Property FontHeader As Font
        Get
            Return _header
        End Get
        Set(value As Font)
            _header = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the font for marked cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>USE THE CLASS MarkedPDFCell IF YOU WANT TO USE THIS ATTRIBUTE. Default value is Arial, 11pt, bold, black.</remarks>
    Public Property FontContentBold As Font
        Get
            Return _contentbold
        End Get
        Set(value As Font)
            _contentbold = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the font for marked cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>USE THE CLASS MarkedPDFCell IF YOU WANT TO USE THIS ATTRIBUTE. Default value is Arial, 11pt, normal, red.</remarks>
    Public Property FontContentSpecial As Font
        Get
            Return _contentspecial
        End Get
        Set(value As Font)
            _contentspecial = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the backgroundcolors of the content cells row wise.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Just add the first series. To set the background of the header, use HeaderBackground-Property. Default is white</remarks>
    Public Property IteratingCellBackground As Collection(Of BaseColor)
        Get
            Return _cellbackground
        End Get
        Set(value As Collection(Of BaseColor))
            _cellbackground = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the backgroundcolor of the header cells.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default value is black</remarks>
    Public Property HeaderBackground As BaseColor
        Get
            Return _headerBackground
        End Get
        Set(value As BaseColor)
            _headerBackground = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the border widths of the header cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default values are 0.5</remarks>
    Public Property HeaderBorderWidths As CellBorders(Of Single)
        Get
            Return _headerBorderWidths
        End Get
        Set(value As CellBorders(Of Single))
            _headerBorderWidths = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the border colors of the header cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default values are black</remarks>
    Public Property HeaderBorderColors As CellBorders(Of BaseColor)
        Get
            Return _headerBorderColors
        End Get
        Set(value As CellBorders(Of BaseColor))
            _headerBorderColors = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the border widths of the content cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default values are 0.5</remarks>
    Public Property ContentBorderWidths As CellBorders(Of Single)
        Get
            Return _contentBorderWidths
        End Get
        Set(value As CellBorders(Of Single))
            _contentBorderWidths = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the border colors of the content cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default values are black</remarks>
    Public Property ContentBorderColors As CellBorders(Of BaseColor)
        Get
            Return _headerBorderColors
        End Get
        Set(value As CellBorders(Of BaseColor))
            _contentBorderColors = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the border widths of the last row's cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default values are 0.5</remarks>
    Public Property LastRowBorderWidths As CellBorders(Of Single)
        Get
            Return _lastRowBorderWidths
        End Get
        Set(value As CellBorders(Of Single))
            _lastRowBorderWidths = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the border colors of the last row's cells
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default values are black</remarks>
    Public Property LastRowBorderColors As CellBorders(Of BaseColor)
        Get
            Return _LastRowBorderColors
        End Get
        Set(value As CellBorders(Of BaseColor))
            _lastRowBorderColors = value
        End Set
    End Property

    ''' <summary>
    ''' Represents the backgroundcolor of the last row's cells.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Default value is black</remarks>
    Public Property LastRowBackground As BaseColor
        Get
            Return _headerBackground
        End Get
        Set(value As BaseColor)
            _headerBackground = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets if the header is on every new page.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property HeaderOnNewPages As Boolean
        Get
            Return _headerOnNewPage
        End Get
        Set(value As Boolean)
            _headerOnNewPage = value
        End Set
    End Property

    ''' <summary>
    ''' Decide if the background color is taken from "IteratingCellBackground" (true) or "LastRowBackground" (false)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property TakeLastRowBackgroundColorFromContentSeries As Boolean
        Get
            Return _takeLastRowBackgroundFromContentSeries
        End Get
        Set(value As Boolean)
            _takeLastRowBackgroundFromContentSeries = value
        End Set
    End Property

#End Region

#Region "Methodes"
    Public Function GetCurrentCellBackground(ByVal rowindex As Integer) As BaseColor
        If IteratingCellBackground.Count = 0 Then
            Return BaseColor.WHITE
        End If
        Return IteratingCellBackground(rowindex Mod IteratingCellBackground.Count)
    End Function
#End Region
End Class

Public Class MarkedPDFCell
    Private _data As Object
    Private _type As BoldOrSpecial
    Public Sub New(ByRef data As Object, type As BoldOrSpecial)
        _data = data
        _type = type
    End Sub

    Public Property Data As Object
        Get
            Return _data
        End Get
        Set(value As Object)
            _data = value
        End Set
    End Property

    Public Property Type As BoldOrSpecial
        Get
            Return _type
        End Get
        Set(value As BoldOrSpecial)
            _type = value
        End Set
    End Property
End Class

Public Enum BoldOrSpecial
    Bold = 0
    Special = 1
End Enum

Public Class CellBorders(Of T)
    Dim _top, _bottom, _left, _right As T

    Public Sub New()
    End Sub

    ''' <summary>
    ''' Creates new instance of CellBorders
    ''' </summary>
    ''' <param name="top">Top Thickness</param>
    ''' <param name="bottom">Bottom Thickness</param>
    ''' <param name="left">Left Thickness</param>
    ''' <param name="right">Right Thickness</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal top As T, ByVal bottom As T, ByVal left As T, ByVal right As T)
        _top = top
        _bottom = bottom
        _left = left
        _right = right
    End Sub

    ''' <summary>
    ''' Creates new instance of CellBorders
    ''' </summary>
    ''' <param name="All">Overall Thickness</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal All As T)
        _top = All
        _bottom = All
        _left = All
        _right = All
    End Sub

    Public Property Top As T
        Get
            Return _top
        End Get
        Set(value As T)
            _top = value
        End Set
    End Property

    Public Property Bottom As T
        Get
            Return _bottom
        End Get
        Set(value As T)
            _bottom = value
        End Set
    End Property

    Public Property Left As T
        Get
            Return _left
        End Get
        Set(value As T)
            _left = value
        End Set
    End Property

    Public Property Right As T
        Get
            Return _right
        End Get
        Set(value As T)
            _right = value
        End Set
    End Property
End Class